package com.hc.radius.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.hc.bean.RoleBean;
import com.hc.radius.command.CommandParam;
import com.hc.radius.command.CommandWrap;

public class RaduistClient {

	private Socket sk;
	private final static int MAX_TIMEOUT = 100;

	// 你自己实现 解析数据
	private DataParse dataParse = new NullDataParse();
	private final RETN retn = new RETN();
    private final CommandParam cmdp = new CommandParam();
	public void setdataParse(DataParse dataParse) {
		if (dataParse != null)
		this.dataParse = dataParse;
	}

	public boolean isConnect(String ip, int port) {
		try {
			sk = new Socket(ip, port);
		} catch (UnknownHostException e) {
			// e.printStackTrace();
			return false;
		} catch (IOException e) {
			return false;
		} finally {
			if (sk != null) {
				try {
					sk.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}
	
	private void preSend(Class clazz,CommandParam cmdp) {
		reset();
		byte[] data = CommandWrap.wrap(clazz, cmdp);
		if (data == null || data.length == 0) {
			return;
		}
//		print(data);
		send(data);
		try {
			syncWait();
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void send(byte[] data) {
		try {
			sk.getOutputStream().write(data);
			sk.getOutputStream().flush();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	private void reset() {
		retn.isBegin = 0;
		retn.data  = "";
		retn.status = false;
	}
	
	
	private void waitTime(long time) throws InterruptedException {
		Thread.sleep(time);
	}
	
	public boolean login(String name, String passwd) {
		cmdp.name = name;
		cmdp.pswd = passwd+";";
		preSend(CommandWrap.lognCmd,cmdp);
		return retn.status;
	}

	public boolean logout(String name) {
		cmdp.name = name;
		preSend(CommandWrap.logoutCmd,cmdp);
		return retn.status;
	}

	public RoleBean queryuser(String name) {
		cmdp.queryname = name+";";
		preSend(CommandWrap.qusCmd,cmdp);
		return dataParse.parseToPPPOEUSER(retn.data);
	}

	 
	public Order queryorder(String name) {
		cmdp.queryname = name+",";
		preSend(CommandWrap.qoCmd,cmdp);
		return dataParse.parseToOrder(retn.data);
	}

	private void syncWait() throws InterruptedException {
		int timeout = 0;
		while (retn.isBegin == 0 && timeout < MAX_TIMEOUT) {
			waitTime(50);
			timeout++;
		}
	}

	public void connect(String ip, int port) throws UnknownHostException,
			IOException {
		sk = new Socket(ip, port);
		new Thread(new Rec(sk)).start();
	}

	public void close() {
		if (sk != null) {
			try {
				sk.getOutputStream().close();
				sk.getInputStream().close();
				sk.close();
				sk = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	class Rec implements Runnable {
		Socket sk;
		int len = 0;
		byte[] buf = new byte[1024 * 1024];

		public Rec(Socket sk) {
			this.sk = sk;
		}

		@Override
		public void run() {
			InputStream is;
			try {
				is = sk.getInputStream();
				String content = null;
				while ((len = is.read(buf)) != -1) {
					content = new String(buf, 0, len);
					retn.data = content;
					retn.status = dataParse.handler(content);
					retn.isBegin = 1;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	class RETN {
		boolean status = false;
		String data;
		int isBegin = 0;
	}

//	private void print(byte[] data) {
//		int k = 0;
//		for (int i = 0; i < data.length - 1; i = i + 2) {
//
//			System.out
//					.print(toHexString(data[i] >= 0 ? data[i] : 256 + data[i]));
//			System.out.print(toHexString(data[i + 1] >= 0 ? data[i + 1]
//					: 256 + data[i + 1]));
//			System.out.print(" ");
//			if (k != 0 && (k + 1) % 4 == 0) {
//				System.out.println();
//			}
//			k++;
//		}
//		System.out.println();
//		System.out.println("原始数据:" + new String(data, 0, data.length));
//		System.out.println("数据段长度:" + (data.length - 12));
//	}
//
//	private String toHexString(int b) {
//		String val = Integer.toHexString(b);
//		if (val.equals("0")) {
//			val += "0";
//		}
//		return val;
//	}

}
